package com.apply.controller;

import com.apply.config.Results;
import com.apply.config.web.SidAuthContext;
import com.apply.entity.ApplyInfo;
import com.apply.entity.ExamInfo;
import com.apply.entity.SysUser;
import com.apply.service.ApplyService;
import com.apply.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author tianp
 */
@RestController
@RequestMapping("/private/apply/")
@Api(value = "apply", tags = "报名")
public class ApplyController {
    @Autowired
    private ApplyService applyService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "考试报名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examId", value = "考试id", dataType = "long", required = true),
            @ApiImplicitParam(name = "applyInfo", value = "报名信息", dataType = "ApplyInfo"),
    })
    @PostMapping("applyExam")
    public Results applyExam(HttpServletRequest request, ApplyInfo applyInfo,
                             @RequestParam(name = "examId", required = true) Long examId) {
        SysUser sysUser = SidAuthContext.get();
        applyService.applyExam(applyInfo, sysUser.getId(), examId);
        return Results.success();
    }

    @ApiOperation(value = "文件上传")
    @ApiImplicitParam(name = "file", value = "文件")
    @PostMapping("fileUpload")
    public Results fileUpload(@RequestParam(name = "file") MultipartFile file, HttpServletRequest request) throws Exception {
        String sid = request.getHeader("sid");
        return Results.success(applyService.uploadFile(file, sid));
    }

    @ApiOperation(value = "我的报名信息")
    @GetMapping("findBySysUserId")
    public Results findBySysUserId() {
        SysUser user = SidAuthContext.get();
        if (null == user || null == user.getId()) {
            return Results.error("登录信息已失效！！！");
        }
        return Results.success(applyService.findBySysUserId(user.getId()));
    }

    @ApiOperation(value = "分页查询报名信息")
    @GetMapping("pageQueryApplyInfo")
    public Results pageQueryApplyInfo(ApplyInfo applyInfo,
                                      @RequestParam(value = "page", defaultValue = "0") int page,
                                      @RequestParam(value = "page", defaultValue = "10") int pageSize) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        return Results.success(userService.pageUserApplyInfo(applyInfo, new PageRequest(page, pageSize, sort)));
    }

    @ApiOperation(value = "判断是否可以报名考试")
    @ApiImplicitParam(name = "examId", value = "考试id", required = true, dataType = "long")
    @GetMapping("canApply")
    public Results canApply(Long examId) {
        SysUser user = SidAuthContext.get();
        return Results.success(applyService.canApply(examId, user.getId()));
    }
}
